<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>删除有序链表中重复出现的元素</title>
</head>
<body>
删除有序链表中重复出现的元素
<br>
给出一个升序排序的链表，删除链表中的所有重复出现的元素，只保留原链表中只出现一次的元素。
例如：
给出的链表为1 \to 2\to 3\to 3\to 4\to 4\to51→2→3→3→4→4→5, 返回1\to 2\to51→2→5.
给出的链表为1\to1 \to 1\to 2 \to 31→1→1→2→3, 返回2\to 32→3.
<script type="text/javascript" src="./js/LinkedList1.js"></script>
<script type="text/javascript">
function deleteDuplicates( head ) {
    // write code here
    if(head == null || head.next == null) { 
        return head;
    }
    const top = {};
    top.next = head;
    let cur = top;
    while(cur.next && cur.next.next) {
        if(cur.next.val == cur.next.next.val) {
            // 当两个相邻的值相同时,取出其val
            let val = cur.next.val;
            //继续遍历,依次删除所有val
            while(cur.next && cur.next.val == val) {
                cur.next = cur.next.next;
            }
        }else{
            cur = cur.next;
        }
    }

    return top.next;
}
var obj = new LinkedList();
obj.addAtHead(11);
obj.addAtHead(11);
obj.addAtHead(11);
obj.addAtTail(12);
obj.addAtTail(13);
obj.addAtTail(14);
deleteDuplicates(obj.head);
console.log(obj.toString());
</script>
</body>
</html>